<?php
    session_start();
    include 'connectdb.php';
    
    $c_id = $_SESSION['id'];
    $h_r_id = $_POST['h_r_id'];
    $date = Date('Y-m-d');
    $date1 = $_POST['h_date1'];
    $date2 = $_POST['h_date2'];
    $price = $_POST['h_price'];
    $deposit = 0;
    $night = $_POST['h_n'];
    echo $price_per_date = $price / $night;
    
    $q = "insert into booking (customer_id,booking_date,total_price,deposit) 
        values ('$c_id','$date','$price','$deposit')";
    mysqli_query($link, $q);
    
    $q2 = "select MAX(booking_id) as max_id from booking where customer_id =".$c_id." 
            and booking_date ='".$date."' and total_price ='".$price."' and deposit ='".$deposit."'";
    $result2 = mysqli_query($link, $q2);
    $row = mysqli_fetch_array($result2);
    
    $booking_id = $row['max_id'];
    
    while (strtotime($date1) < strtotime($date2)){
        $q3 = "insert into booking_detail (booking_id,home_stay_room_id,date,price,customer_id,status_id) 
            values ('$booking_id','$h_r_id','$date1','$price_per_date','$c_id',1)";
        mysqli_query($link, $q3);
        $date1 = date ("Y-m-d", strtotime("+1 day", strtotime($date1)));
    }
    echo '<script language="javascript">alert("บันทึกการจองเรียบร้อยแล้วค่ะ กรุณาชำระเงินมัดจำ ภายใน 48 ชั่วโมง");</script>';
    echo '<script language="javascript">window.location = "index.php";</script>';
?>
